package com.young.springdemo.service.statePattern;

//账户的正常状态
public class NormalAccountState extends AccountState {

    public NormalAccountState(Account account) {
        this.account = account;
    }

    public NormalAccountState(AccountState accountState) {
        this.account = accountState.account;
    }

    @Override
    public void deposit(double amount) { //存款
        System.out.println(this.account.getOwner() + "存款" + amount);
        account.setBalance(this.account.getBalance() + amount);
        stateCheck();
    }


    @Override
    public void withdraw(double amount) { //取款
        System.out.println(this.account.getOwner() + "取款" + amount);
        account.setBalance(account.getBalance() - amount);
        stateCheck();
    }

    @Override
    public void computerInterest() {
        System.out.println(
                "正常状态,无需计算利息"
        );
    }

    @Override
    public void stateCheck() {
        if (account.getBalance() > -2000 && account.getBalance() <= 0 ){
            account.setState(new OverDraftAccountState(this));
            System.out.println("切换为透支状态");
        }else if (account.getBalance() < -2000){
            account.setState(new RestrictedStateAccountState(this));
            System.out.println("切换为受限状态");
        }
    }
}
